package com.iiot.manage.controller;

import com.iiot.manage.exception.ResponseBean;
import com.iiot.manage.exception.ResponseErrorCodeEnums;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class SimpleErrorController implements ErrorController {

    private static final String ERROR_PATH="/error";
    @RequestMapping("/error")
    public ResponseBean<Object> handleError(HttpServletRequest request){
        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if(statusCode==404){
            return new ResponseBean<>(false,null, ResponseErrorCodeEnums.NOT_FOUND);
        }else {
            return new ResponseBean<>(false,null,ResponseErrorCodeEnums.METHOD_NOT_ALLOWED);
        }

    }
    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

}
